package com.itheima.service.impl;

import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public PageInfo pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<CheckGroup> checkGroups = checkGroupDao.findByCondition(queryPageBean.getQueryString());
        return new PageInfo(checkGroups);
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroup_id",checkGroup.getId());//1
            map.put("checkItem_id",checkitemId);//checkitemIds 1 2 3
            checkGroupDao.setCheckGroupAndCheckItems(map);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.update(checkGroup);
        checkGroupDao.deleteCheckGroup_CheckItems(checkGroup.getId());
        for (Integer checkitemId : checkitemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroup_id",checkGroup.getId());
            map.put("checkItem_id",checkitemId);
            checkGroupDao.setCheckGroupAndCheckItems(map);
        }
    }

    @Override
    public void deleteById(Integer id) {
        checkGroupDao.deleteCheckGroup_CheckItems(id);
        checkGroupDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findall() {
        return checkGroupDao.findall();
    }

    @Override
    public List<Integer> findCheckGroupsBySetMealId(Integer smId) {
        return checkGroupDao.findCheckGroupIdsBySetMealId(smId);
    }
}
